﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Transactions;
using context;
using entities;

namespace DataAccessLibrary.DAL
{
    public class RoomDAO
    {
        public List<EquipmentDetail> SelectEquipmentsDetails(int available)
        {
            using (var ctx = new BlueMDataContext())
            {
                IQueryable<EquipmentDetail> query = ctx.EquipmentDetails;

                if (available > -1)
                {
                    query = query.Where(p => p.Available == Convert.ToBoolean(available));
                }
                return query.ToList();
            }
        }


        public EquipmentDetail SelectEquipmentDetail(int equipmentDetailID)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.EquipmentDetails.First(p => p.EquipmentDetailID == equipmentDetailID);
            }
        }

        public List<EquipmentDetail> SelectEquipmentDetailsByRoomTypeID(int roomTypeId)
        {
            using (var ctx = new BlueMDataContext())
            {
                RoomType roomType = ctx.RoomTypes.First(p => p.RoomTypeID == roomTypeId);
                return roomType.Equipments.Select(p => p.EquipmentDetail).ToList();
            }
        }

        public List<EquipmentDetail> SelectEquipmentsDetails(string equipmentName)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.EquipmentDetails.Where(p => p.EquipmentName.Contains(equipmentName)).ToList();
            }
        }

        public bool InsertEquipmentDetail(EquipmentDetail equipmentDetail)
        {
            using (var ctx = new BlueMDataContext())
            {
                ctx.EquipmentDetails.InsertOnSubmit(equipmentDetail);
                ctx.SubmitChanges();
            }

            return true;
        }

        public bool UpdateEquipmentDetail(EquipmentDetail equipmentDetail)
        {
            using (var ctx = new BlueMDataContext())
            {
                EquipmentDetail ued =
                    ctx.EquipmentDetails.First(p => p.EquipmentDetailID == equipmentDetail.EquipmentDetailID);
                ued.EquipmentName = equipmentDetail.EquipmentName;
                ued.EquipmentPrice = equipmentDetail.EquipmentPrice;
                ued.Available = equipmentDetail.Available;
                ctx.SubmitChanges();
            }

            return true;
        }

        public RoomType SelectRoomType(int roomTypeID)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.RoomTypes.First(p => p.RoomTypeID == roomTypeID);
            }
        }

        public List<RoomType> SelectRoomType()
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.RoomTypes.ToList();
            }
        }

        public bool InsertRoomType(RoomType roomType, IEnumerable<int> equipmentDetailIDList)
        {
            using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 0, 30)))
            {
                using (var ctx = new BlueMDataContext())
                {
                    ctx.RoomTypes.InsertOnSubmit(roomType);
                    ctx.SubmitChanges();

                    List<Equipment> list = equipmentDetailIDList.Select(id => new Equipment {RoomTypeID = roomType.RoomTypeID, EquipmentDetailID = id}).ToList();
                    ctx.Equipments.InsertAllOnSubmit(list);
                    ctx.SubmitChanges();

                    ts.Complete();
                    return true;
                }
            }

        }

        public bool UpdateRoomType(RoomType roomType, IEnumerable<int> equipmentDetailIds)
        {
            using (var ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 0, 30)))
            {
                using (var ctx = new BlueMDataContext())
                {
                    RoomType urt = ctx.RoomTypes.First(p => p.RoomTypeID == roomType.RoomTypeID);
                    urt.RoomPrice = roomType.RoomPrice;
                    urt.RoomRank = roomType.RoomRank;
                    urt.RoomTypeTitle = roomType.RoomTypeTitle;
                    urt.View = roomType.View;
                    urt.Balcony = roomType.Balcony;
                    urt.Bath = roomType.Bath;
                    ctx.SubmitChanges();
                    
                    ctx.Equipments.DeleteAllOnSubmit(urt.Equipments);
                    ctx.SubmitChanges();

                    var lst = equipmentDetailIds.Select(p => new Equipment {EquipmentDetailID = p,RoomTypeID = urt.RoomTypeID}).ToList();
                    ctx.Equipments.InsertAllOnSubmit(lst);
                    ctx.SubmitChanges();
                    ts.Complete();
                }
                return true;
            }
        }

        public List<Room> SelectRooms(int status)
        {
            using (var ctx = new BlueMDataContext())
            {
                IQueryable<Room> query = ctx.Rooms;

                if (status <= 1 || status >= -1)
                {
                    query = query.Where(p => p.Status == status);
                }
                return query.ToList();
            }
        }

        public Room SelectRoom(int roomId)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.Rooms.FirstOrDefault(p => p.RoomID == roomId);
            }
        }

        public List<Room> SelectRoomsByRoomTypeID(int roomTypeId)
        {
            return SelectRoomType(roomTypeId).Rooms.ToList();
        }

        public bool InsertRoom(Room room)
        {
            using (var ctx = new BlueMDataContext())
            {
                ctx.Rooms.InsertOnSubmit(room);
                ctx.SubmitChanges();
            }

            return true;
        }

        public bool UpdateRoom(Room room)
        {
            using (var ctx = new BlueMDataContext())
            {
                Room ur = ctx.Rooms.First(p => p.RoomID == room.RoomID);
                ur.RoomTypeID = room.RoomTypeID;
                ur.Status = room.Status;
                ctx.SubmitChanges();
            }

            return true;
        }
    }
}